﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Transports.PresentationLayer;
using Transports.Logging;
namespace Transports.Domain
{
    class Domain
    {
        private ILogger logger;

        private static Creator[] creators;

        public Domain(ILogger logger)
        {
            route = new Route();
            creators = new Creator[4];
            creators[0] = new PlaneCreator();
            creators[1] = new HelicopterCreator();
            creators[2] = new TruckCreator();
            creators[3] = new PassengerCarCreator();
            this.logger = logger;
            logger.Log("Начало работы");
        }

        ~Domain()
        {
            logger.Log("Конец работы");
        }

        private Route route;

        private void ReadAll(IO RecordPresentation)
        {

            while (!RecordPresentation.EOF())
            {
                string str = RecordPresentation.ReadLine();
                string[] split = str.Split(',');
                switch (split[0])
                {
                    case "Самолет":
                        route.AddTransport(creators[0].FactoryMethod(uint.Parse(split[1]), split[2], uint.Parse(split[3]), uint.Parse(split[4])));
                        break;
                    case "Вертолет":
                        route.AddTransport(creators[0].FactoryMethod(uint.Parse(split[1]), split[2], uint.Parse(split[3]), uint.Parse(split[4])));
                        break;
                    case "Грузовик":
                        route.AddTransport(creators[0].FactoryMethod(uint.Parse(split[1]), split[2], uint.Parse(split[3]), uint.Parse(split[4])));
                        break;
                    case "Легковой автомобиль":
                        route.AddTransport(creators[0].FactoryMethod(uint.Parse(split[1]), split[2], uint.Parse(split[3]), uint.Parse(split[4])));
                        break;
                }

            }
        }

        public void UsingMenu (IO MenuPresentation, IO RecordPresentation)
        {
            bool flag = true;
            while (flag)
            {
                MenuPresentation.WriteLine("Введите нужную цифру:");
                MenuPresentation.WriteLine("1 - Чтение даных");
                MenuPresentation.WriteLine("2 - Изменение данных");
                MenuPresentation.WriteLine("3 - Запись данных");
                MenuPresentation.WriteLine("4 - Вывод информации");
                MenuPresentation.WriteLine("5 - Выход");
                MenuPresentation.Pause();
                char ch = MenuPresentation.ReadChar();
                MenuPresentation.WriteLine("");
                switch (ch)
                {
                    case '1':
                        {
                            ReadAll(RecordPresentation);
                        }
                        break;
                    case '2':
                        {
                            bool flag2 = true;
                            while (flag2)
                            {
                                MenuPresentation.WriteLine("Введите нужную цифру:");
                                MenuPresentation.WriteLine("1 - Добавление нового транспорта");
                                MenuPresentation.WriteLine("2 - Очистить маршрут");
                                MenuPresentation.WriteLine("3 - На уровень вверх");
                                MenuPresentation.Pause();
                                char ch1 = MenuPresentation.ReadChar();
                                switch (ch1)
                                {
                                    case '1':
                                        {
                                            bool flag3 = true;

                                            MenuPresentation.WriteLine("Введите данные");
                                            string str = MenuPresentation.ReadLine();
                                            string[] split = str.Split(' ');
                                            while (flag3)
                                            {
                                                MenuPresentation.WriteLine("Введите нужную цифру:");
                                                MenuPresentation.WriteLine("1 - Самолет");
                                                MenuPresentation.WriteLine("2 - Вертолет");
                                                MenuPresentation.WriteLine("3 - Грузовик");
                                                MenuPresentation.WriteLine("4 - Легковой автомобиль");
                                                MenuPresentation.WriteLine("5 - На уровень вверх");
                                                MenuPresentation.Pause();
                                                char ch2 = MenuPresentation.ReadChar();
                                                switch (ch2)
                                                {
                                                    case '1':
                                                        route.AddTransport(creators[0].FactoryMethod(uint.Parse(split[0]), split[1], uint.Parse(split[0]), uint.Parse(split[0])));
                                                        break;
                                                    case '2':
                                                        route.AddTransport(creators[0].FactoryMethod(uint.Parse(split[0]), split[1], uint.Parse(split[0]), uint.Parse(split[0])));
                                                        break;
                                                    case '3':
                                                        route.AddTransport(creators[0].FactoryMethod(uint.Parse(split[0]), split[1], uint.Parse(split[0]), uint.Parse(split[0])));
                                                        break;
                                                    case '4':
                                                        route.AddTransport(creators[0].FactoryMethod(uint.Parse(split[0]), split[1], uint.Parse(split[0]), uint.Parse(split[0])));
                                                        break;
                                                    case '5':
                                                        flag3 = false;
                                                        break;
                                                    default:
                                                        MenuPresentation.WriteLine("");
                                                        MenuPresentation.WriteLine("Введен неправильный символ");
                                                        MenuPresentation.Pause();
                                                        break;
                                                }
                                            }
                                        }
                                        break;
                                    case '2':
                                        {
                                            route.Clear();
                                        }
                                        break;
                                    case '3':
                                        {
                                            flag2 = false;
                                        }
                                        break;
                                    default:
                                        {
                                            MenuPresentation.WriteLine("");
                                            MenuPresentation.WriteLine("Введен неправильный символ");
                                            MenuPresentation.Pause();
                                        }
                                        break;
                                }
                            };
                        }
                        break;
                    case '3':
                        {
                            MenuPresentation.WriteLine("");
                            for (int i = 0; i < route.NumberOfTransport(); i++)
                            {
                                RecordPresentation.Write(route.Transport(i).GetName());
                                RecordPresentation.Write(" ");
                                RecordPresentation.Write(Convert.ToString(route.Transport(i).GetFare()));
                                RecordPresentation.WriteLine("");
                            }
                            RecordPresentation.Write("Общая стоимость ");
                            RecordPresentation.WriteLine((Convert.ToString(route.GetTotalFare())));
                        }
                        break;
                    case '4':
                        {
                            for (int i = 0; i < route.NumberOfTransport(); i++)
                            {
                                MenuPresentation.Write(route.Transport(i).GetName());
                                MenuPresentation.Write(" ");
                                MenuPresentation.Write(Convert.ToString(route.Transport(i).GetFare()));
                                MenuPresentation.WriteLine("");
                            }
                            MenuPresentation.Write("Общая стоимость ");
                            MenuPresentation.WriteLine((Convert.ToString(route.GetTotalFare())));
                        }
                        break;
                    case '5':
                        {
                            flag = false;
                            MenuPresentation.WriteLine("");
                        }
                        break;
                    default:
                        {
                            MenuPresentation.WriteLine("");
                            MenuPresentation.WriteLine("Введен неправильный символ");
                            MenuPresentation.Pause();
                        }
                        break;
                }
            }
        }

    }
}
